#include "RenderingEngine.h"

using namespace std;
using namespace sf;
RenderingEngine::RenderingEngine(RenderWindow &window, SpriteManager &SpriteManager) :
_window(window),
_SpriteManager(SpriteManager)
{}






void  RenderingEngine::drawCharacterMap(const map <int, Character*> &characterMap)
{



	for (auto it : characterMap) 
	{
		if (it.second->getAlive())
		{
		/*	RectangleShape rectangle;
			rectangle.setPosition(it.second->getPosition());
			rectangle.setSize(it.second->getSize());
			_window.draw(rectangle);*/
			_SpriteManager.setTextureOnCharacter(*it.second);
			_window.draw(_SpriteManager.getShadowSprite());
		   _window.draw(_SpriteManager.getSprite());				
		   _SpriteManager.setTurret(*it.second);
		   _window.draw(_SpriteManager.getShadowSprite());
		   _window.draw(_SpriteManager.getSprite());
		   
		  
		}
	
	}
}

void RenderingEngine::drawBulletMap(const map <int, Character*>&characterMap)
{
	for (auto it :characterMap)
	{

		for (auto it2 : it.second->getBulletManager().getBulletMap())
		{
			if (it2.second->getIsShooting())
			{
				
				_SpriteManager.setTextureOnBullet(*it2.second);				
				_window.draw(_SpriteManager.getSprite());
			
			}

		}
	}
}

void RenderingEngine::drawBackground(int level)
{
	_SpriteManager.setBackground(level);
	_window.draw(_SpriteManager.getSprite());
}

void RenderingEngine::drawSimpleSprite(sf::Vector2f position, sf::Vector2f scale, SpriteEnum sprite)
{
	_SpriteManager.setSprite(position,scale,sprite);
	_window.draw(_SpriteManager.getSprite());
}


void  RenderingEngine::clear(sf::Color color)
{

	_window.clear(color);

}

void  RenderingEngine::display()
{
	
	_window.display();
	
}

void RenderingEngine::drawRectangle(const RectangleShape &shape)
{
	_window.draw(shape);

}


void RenderingEngine::changeCursor(CircleShape &newCursor, const Vector2f &mousePosition)
{

	_window.setMouseCursorVisible(false);
	newCursor.setPosition(sf::Vector2f((mousePosition.x + newCursor.getRadius()*2), mousePosition.y + newCursor.getRadius()*2));
	_window.draw(newCursor);

}

void RenderingEngine::drawText(std::string texte, int size, sf::Color Color, sf::Vector2f position)
{
	_SpriteManager.setText(texte, size, Color, position);
	_window.draw(_SpriteManager.getText());
}






void RenderingEngine::endGame(bool victory)
{
	Image capture=_window.capture();
	Sprite captureSprite;
	Texture fondCapture;
	fondCapture.loadFromImage(capture);
	captureSprite.setTexture(fondCapture);
	RectangleShape rectangle(Vector2f(900,100));
	rectangle.setPosition(Vector2f(0, 150));
	rectangle.setFillColor(Color::Black);
	rectangle.setRotation(10);

	Text text1;	
	Font font;
	font.loadFromFile("./media/TravelingTypewriter.ttf");
	text1.setFont(font);
	text1.setCharacterSize(50);
	text1.setColor(Color::White);
	text1.setPosition(Vector2f(0, 165));

	
	text1.setRotation(10);
	

	if (victory)
	{
		text1.setString("               VICTORY");
		captureSprite.setColor(Color(0, 255, 0));
	}
	else
	{		
		text1.setString("               DEFEAT");
		captureSprite.setColor(Color(255, 0, 0));
	}

	for (int i = 0; i <= 5000; i++)
	{
		
	_window.clear(Color::Black);		
	_window.draw(captureSprite);
	_window.draw(rectangle);
	_window.draw(text1);	
	_window.display();
	}

	

	
}

void RenderingEngine::drawVideo()
{	
	if (readingNormalDirection)
	{
		videoTimer++;
		if (videoTimer == 50)
		{
			readingNormalDirection = false;
		}
	}

	else
	{		
		videoTimer--;
		if (videoTimer == 1)
		{
			
			readingNormalDirection = true;
		}
	}

	_SpriteManager.setVideoOnSprite(videoTimer);
	_window.draw(_SpriteManager.getSprite());
	

}